//
//  TextField.m
//  lumihui
//
//  Created by oceans on 2018/10/17.
//  Copyright © 2018 ios. All rights reserved.
//

#import "TextField.h"

@implementation TextField

- (instancetype)initWithFrame:(CGRect)frame type:(TextFieldType)type {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupWithType:type];
    }
    return self;
}

- (void)setupWithType:(TextFieldType)type {
    
    self.borderStyle = UITextBorderStyleNone;
    
    CALayer *line = [[CALayer alloc] init];
    line.bounds = CGRectMake(0, 0, self.bounds.size.width, 0.5);
    line.backgroundColor = [UIColor colorWithRed:226/255.0 green:225/255.0 blue:226/255.0 alpha:1.0].CGColor;
    line.position = CGPointMake(self.bounds.size.width*0.5, self.bounds.size.height-0.5);
    [self.layer addSublayer:line];
    
    if (type == TFTNoLeftRightView) {
        return;
    }else if (type == TFTLeftView) {
        [self setupLeftImageView];
    }else if (type == TFTLeftRightView) {
        [self setupLeftImageView];
        [self setupRightView];
    }else {
        [self setupLeftImageView];
        [self setupCountDownButton];
    }
}

- (void)setupLeftImageView {
    
    UIView *left = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50.0, self.bounds.size.height)];
//    _leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 23, 23)];
//    _leftImageView.center = CGPointMake(25.0, self.bounds.size.height*0.5);
//    [left addSubview:_leftImageView];
    _leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(3, 0, 47, self.bounds.size.height)];
    _leftLabel.font = [UIFont systemFontOfSize:12.0];
    _leftLabel.textColor = COLOR(51, 51, 51, 1);
    [left addSubview:_leftLabel];
    self.leftView = left;
    self.leftViewMode = UITextFieldViewModeAlways;
    
}

- (void)setupRightView {
    
    UIView *right = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50.0, self.bounds.size.height)];
    _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightButton.frame = CGRectMake(0, 0, 25.0, 25.0);
    _rightButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
    _rightButton.center = CGPointMake(25.0, self.bounds.size.height*0.5);
    [right addSubview:_rightButton];
    self.rightView = right;
    self.rightViewMode = UITextFieldViewModeAlways;
}

- (void)setupCountDownButton {
    
    UIView *right = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, self.bounds.size.height)];
    _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightButton.frame = CGRectMake(0, 0, 90, 23);
    _rightButton.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [_rightButton setTitleColor:COLOR(243, 185, 157, 1) forState:UIControlStateNormal];
    _rightButton.center = CGPointMake(50.0, self.bounds.size.height*0.5);
    [right addSubview:_rightButton];
    self.rightView = right;
    self.rightViewMode = UITextFieldViewModeAlways;
}

@end
